home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: uu4news.netcom.com!zodiac!szh
- From: szh@zcon.com (Syed Zaeem Hosain)
- Subject: Re: Can someone help me with fopen
- Message-ID: <1996Feb15.173508.13317@zcon.com>
- Sender: szh@zcon.com (Syed Zaeem Hosain)
- Nntp-Posting-Host: zodiac
- Reply-To: szh@zcon.com
- Organization: Z Consulting Group
- References: <4fouoo$k0e@news.uncc.edu>
- Date: Thu, 15 Feb 1996 17:35:08 GMT
-
- In article <4fouoo$k0e@news.uncc.edu>, Samuel S Feeback <ssfeebac> writes:
- >Here is my problem. I am trying to read a text file, search for a certain
- >string and write that string to an outfile. I am having trouble with the fopen
- >commnad. Here is a fragment of my code.
- >
- >char input; /* variable to put filename in*/
-
- This is a single character. Is the file name short enough to fit
- in this, let alone the null byte you will need to terminate the
- char array? That should give you a hint about how long the variable
- really needs to be! (Was this a pointer, perhaps?)
-
- >FILE *infile /* Pointer to File to be used as input */
- >
- >if (argv[argc] == "-i")
- > {input = ((argv[argc]) + 1);
-
- Yes, you got one character in the above variable "input" here. That is
- not going to work later.
-
- > infile = fopen((("%s", input), "r"));
-
- This call is wrong. What you hand to 'fopen' is something very strange.
- There are way too many parentheses, and some confusion too. Are you
- sure that you were not trying to 'sprintf' the variable first? But
- more importantly, "input" is a single byte, so the filename is not
- correct anyway, most likely.
-
- If "input" were a correct char array (or pointer pointing to a valid
- memory location) containing the file name, then the code for the above
- line would simply look like this:
-
- infile = fopen(input, "r");
-
- My thought is that you are copying code from some other program (or
- book) without thinking too clearly about what is meant to be achieved
- in those lines. Try simplifying the process into simpler steps, read
- the manual pages on the various functions (sprintf, fopen, etc.) and
- then proceed.
-
- > if ((infile = fopen((("%s", input), "r"))) == NULL)
- > {/*Error Message*/
-
- Calling 'fopen' again, to check whether the first one returned NULL is
- probably an error. If the first one succeeded, then the second call is
- not needed.
-
- Again, if the "infile" variable were correctly specified (as I mention
- above), then the following would be better code:
-
- infile = fopen(input, "r");
- if (infile == NULL)
- {
-
- An alternative is to combine the two above, but here is where you have
- to be careful to use the right parentheses to ensure that the fopen
- returned value assignment is done before the comparison (I have put
- dashes and bars to show the parenthesis matching - this ain't part of
- the code!):
-
- ------------------------------------
- | -------------------------- |
- || ---------- | |
- || | || |
- if ((infile = fopen(input, "r")) == NULL)
- {
-
- >I keep getting an "too few arguments to function `fopen'
-
- Yes. That is the difficulty - or at least one of them!
-
- >Please e-mail me is you have any idea on this problem(or any other aspect of
- > this program for that matter :>)
-
- We cannot. Your e-mail address in this post is faulty. Please see the
- reference above (beginning with "In article ...").
-
- Z
-
-
- --
- -------------------------------------------------------------------------
- | Syed Zaeem Hosain P. O. Box 610097 (408) 441-7021 |
- | Z Consulting Group San Jose, CA 95161 szh@zcon.com |
- -------------------------------------------------------------------------
-